package com.net.framework.help.dialog;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.net.framework.help.R;


/**
 * 弹出花样对话框构造
 *
 * @author zhangxiaobin
 */
public class CustomDialogBuilder {
    private Context context;
    private Dialog dialog;
    private final String defTextColor = "#DF1A14";// 默认文本颜色

    private final String defDividerColor = "#DF1A14";// 默认分割线颜色

    private final String defMsgColor = "#ff666666";// 默认消息内容颜色

    private final String defTitle = "提示";// 默认标题

    private LinearLayout mLinearLayoutView;

    private RelativeLayout mRelativeLayoutView;
    private RelativeLayout title_template;

    private LinearLayout mLinearLayoutMsgView;

    private LinearLayout mLinearLayoutTopView;

    private FrameLayout mFrameLayoutCustomView;

    private FrameLayout new_mFrameLayoutCustomView;

    private FrameLayout mFrameLayoutCustomViewNotPadding;

    private View mDialogView;

    private View mDivider;

    private TextView mTitle;

    private TextView mMessage;

    private ScrollView scrollView;

    private ImageView mIcon;

    private View line_vertical;
    private View line_seperator;

    private Button mButton1;

    private Button mButton2;

    private Button mButton3;


    private boolean isCancelable = false;

    private Display display;

    /***
     * 设置默认值
     */
    public CustomDialogBuilder setDefault() {
        mTitle.setTextColor(Color.parseColor(defTextColor));
        mDivider.setBackgroundColor(Color.parseColor(defDividerColor));
        mMessage.setTextColor(Color.parseColor(defMsgColor));
        mTitle.setText(defTitle);
        isCancelableOnTouchOutside(false);
        hideTitle();
        return this;
    }

    public CustomDialogBuilder(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public CustomDialogBuilder builder() {
        mDialogView = LayoutInflater.from(context).inflate(R.layout.layout_fancy_dialog, null);
        // 整个弹出框
        mLinearLayoutView = (LinearLayout) mDialogView.findViewById(R.id.parentPanel);
        mRelativeLayoutView = (RelativeLayout) mDialogView.findViewById(R.id.main);
        mLinearLayoutTopView = (LinearLayout) mDialogView.findViewById(R.id.topPanel);
        mLinearLayoutMsgView = (LinearLayout) mDialogView.findViewById(R.id.contentPanel);
        mFrameLayoutCustomView = (FrameLayout) mDialogView.findViewById(R.id.customPanel);

        new_mFrameLayoutCustomView = (FrameLayout) mDialogView.findViewById(R.id.new_customPanel);
        mFrameLayoutCustomViewNotPadding = (FrameLayout) mDialogView.findViewById(R.id.customPanel_notPadding);

        mTitle = (TextView) mDialogView.findViewById(R.id.alertTitle);
        scrollView = (ScrollView) mDialogView.findViewById(R.id.scrollView);
        mMessage = (TextView) mDialogView.findViewById(R.id.message);
        title_template = (RelativeLayout) mDialogView.findViewById(R.id.title_template);

        mIcon = (ImageView) mDialogView.findViewById(R.id.icon);
        line_vertical = (View) mDialogView.findViewById(R.id.line_vertical);
        line_seperator = (View) mDialogView.findViewById(R.id.line_seperator);
        mDivider = mDialogView.findViewById(R.id.titleDivider);
        mButton1 = (Button) mDialogView.findViewById(R.id.button1);
        mButton2 = (Button) mDialogView.findViewById(R.id.button2);
        mButton3 = (Button) mDialogView.findViewById(R.id.button3);

        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(mDialogView);
        mRelativeLayoutView.setLayoutParams(new FrameLayout.LayoutParams((int) (display.getWidth() * 0.85), LinearLayout.LayoutParams.WRAP_CONTENT));

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                mLinearLayoutView.setVisibility(View.VISIBLE);
            }
        });
        mRelativeLayoutView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isCancelable)
                    dismiss();
            }
        });

        mLinearLayoutView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        return this;
    }


    /********************************************* 设置弹出框的整体背景颜色 ****************************************/
    /***
     * 定义整个控件的背景颜色
     */
    public CustomDialogBuilder setDialogBackground(int resid) {
        mLinearLayoutView.setBackgroundResource(resid);
        return this;
    }

    /********************************************* 设置弹出框的标题 ****************************************/

    /**
     * 设置顶部标题的背景颜色
     *
     * @return
     */
    public CustomDialogBuilder setTitleBackground(int resid) {
        mLinearLayoutTopView.setBackgroundResource(resid);
        return this;
    }

    /***
     * 设置标题
     *
     * @param title
     * @return
     */
    public CustomDialogBuilder setTitle(String title) {
        toggleView(mLinearLayoutTopView, title);
        mTitle.setText(title);
        return this;
    }

    /***
     * 设置标题的字体颜色
     *
     * @param colorString
     * @return
     */
    public CustomDialogBuilder setTitleColor(String colorString) {
        mTitle.setTextColor(Color.parseColor(colorString));
        return this;
    }

    /***
     * 设置标题的字体颜色
     *
     * @return
     */
    public CustomDialogBuilder setTitleColor(int colorresid) {
        mTitle.setTextColor(colorresid);
        return this;
    }

    /***
     * 设置对话框标题字体大小
     *
     * @return
     */
    public CustomDialogBuilder setTitleSize(int sizeresid) {
        mTitle.setTextSize(sizeresid);
        return this;
    }

    /********************************************* 设置弹出框标题于消息之间的间隔线背景颜色 ****************************************/
    /***
     * 标题头和消息之间的间隔线
     *
     * @return
     */
    public CustomDialogBuilder setDividerColor(String colorString) {
        mDivider.setBackgroundColor(Color.parseColor(colorString));
        return this;
    }

    /***
     * 标题头和消息之间的间隔线
     *
     * @return
     */
    public CustomDialogBuilder setDividerColor(int colorresid) {
        mDivider.setBackgroundResource(colorresid);
        return this;
    }

    /********************************************* 设置弹出框中的消息内容 ****************************************/
    /**
     * 设置消息内容
     *
     * @param textResId
     * @return
     */
    public CustomDialogBuilder setMessage(int textResId) {
        toggleView(mLinearLayoutMsgView, textResId);
        if (textResId == 0) {
            mMessage.setVisibility(View.GONE);
            scrollView.setVisibility(View.GONE);
        } else {
            scrollView.setVisibility(View.VISIBLE);
            mMessage.setVisibility(View.VISIBLE);
            mMessage.setText(textResId);
        }
        return this;
    }

    /***
     * 设置消息内容
     *
     * @param msg
     * @return
     */
    public CustomDialogBuilder setMessage(CharSequence msg) {
        toggleView(mLinearLayoutMsgView, msg);
        if (msg.equals("") || msg.length() == 0) {
            scrollView.setVisibility(View.GONE);
            mMessage.setVisibility(View.GONE);
        } else {
            scrollView.setVisibility(View.VISIBLE);
            mMessage.setVisibility(View.VISIBLE);
            mMessage.setText(msg);
        }

        return this;
    }

    /***
     * 设置消息文字的颜色
     *
     * @return
     */
    public CustomDialogBuilder setMessageColor(String colorString) {
        mMessage.setTextColor(Color.parseColor(colorString));
        return this;
    }

    /***
     * 设置消息文字的颜色
     *
     * @return
     */
    public CustomDialogBuilder setMessageColor(int colorresid) {
        mMessage.setTextColor(colorresid);
        return this;
    }

    /***
     * 设置消息字体大小
     *
     * @return
     */
    public CustomDialogBuilder setMessageSize(int sizeresid) {
        mMessage.setTextSize(sizeresid);
        return this;
    }

    /**************************************** 设置弹出框顶部的标题的图标 ***************************/
    /***
     * 设置弹出框顶部的标题的图标
     */
    public CustomDialogBuilder setIcon(int drawableResId) {
        mIcon.setImageResource(drawableResId);
        return this;
    }

    /***
     * 设置弹出框顶部的标题的图标
     *
     * @param icon
     * @return
     */
    public CustomDialogBuilder setIcon(Drawable icon) {
        mIcon.setImageDrawable(icon);
        mIcon.setVisibility(View.VISIBLE);
        return this;
    }

    /********************************************************* 设置确定、取消按钮 *************************************************/
    /***
     * 同时设置两个按钮的背景
     *
     * @param resid
     * @return
     */
    public CustomDialogBuilder setButtonDrawable(int resid) {
        mButton1.setBackgroundResource(resid);
        mButton2.setBackgroundResource(resid);
        mButton3.setBackgroundResource(resid);
        return this;
    }

    /***
     * 设置左边按钮的背景
     *
     * @param resid
     * @return
     */
    public CustomDialogBuilder setButton1Drawable(int resid) {
        mButton1.setBackgroundResource(resid);
        return this;
    }

    /***
     * 设置右边按钮的背景
     *
     * @param resid
     * @return
     */
    public CustomDialogBuilder setButton2Drawable(int resid) {
        mButton2.setBackgroundResource(resid);
        return this;
    }

    /***
     * 设置右边按钮的背景
     *
     * @param resid
     * @return
     */
    public CustomDialogBuilder setButton3Drawable(int resid) {
        mButton3.setBackgroundResource(resid);
        return this;
    }

    /***
     * 设置消息字体大小
     *
     * @return
     */
    public CustomDialogBuilder setButtonSize(int sizeresid) {
        mButton1.setTextSize(sizeresid);
        mButton2.setTextSize(sizeresid);
        mButton3.setTextSize(sizeresid);
        return this;
    }

    /********************************************************** 设置确定、取消按钮的点击事件监听 ***************************************/

    /***
     * 设置左边按钮的文字
     *
     * @param text
     * @return
     */
    public CustomDialogBuilder setCancelButton(CharSequence text, View.OnClickListener click) {
        mButton1.setVisibility(View.VISIBLE);
        line_vertical.setVisibility(View.VISIBLE);
        line_seperator.setVisibility(View.VISIBLE);
        mButton1.setText(text);
        mButton1.setOnClickListener(click);
        return this;
    }

    /***
     * 设置左边按钮的文字
     *
     * @return
     */
    public CustomDialogBuilder setCancelButton(int textId, View.OnClickListener click) {
        mButton1.setVisibility(View.VISIBLE);
        line_vertical.setVisibility(View.VISIBLE);
        line_seperator.setVisibility(View.VISIBLE);
        mButton1.setText(textId);
        mButton1.setOnClickListener(click);
        return this;
    }

    /***
     * 设置右边按钮的文字
     *
     * @param text
     * @return
     */
    public CustomDialogBuilder setOkButton(CharSequence text, View.OnClickListener click) {
        mButton2.setVisibility(View.VISIBLE);
        line_vertical.setVisibility(View.VISIBLE);
        line_seperator.setVisibility(View.VISIBLE);
        mButton2.setText(text);
        mButton2.setOnClickListener(click);
        return this;
    }

    /***
     * 设置右边按钮的文字
     *
     * @return
     */
    public CustomDialogBuilder setOkButton(int textId, View.OnClickListener click) {
        mButton2.setVisibility(View.VISIBLE);
        line_vertical.setVisibility(View.VISIBLE);
        line_seperator.setVisibility(View.VISIBLE);
        mButton2.setText(textId);
        mButton2.setOnClickListener(click);
        return this;
    }

    public CustomDialogBuilder setConfirmButton(CharSequence text, View.OnClickListener click) {
        mButton3.setVisibility(View.VISIBLE);
        line_seperator.setVisibility(View.VISIBLE);
        line_vertical.setVisibility(View.GONE);
        mButton3.setText(text);
        mButton3.setOnClickListener(click);
        return this;
    }

    public CustomDialogBuilder setConfirmButton(int textId, View.OnClickListener click) {
        mButton3.setVisibility(View.VISIBLE);
        line_vertical.setVisibility(View.GONE);
        line_seperator.setVisibility(View.VISIBLE);
        mButton3.setText(textId);
        mButton3.setOnClickListener(click);
        return this;
    }

    /****************************************************** 设置添加视图(左右有边距) ****************************************/
    /****
     * 设置添加额外的视图
     *
     * @param resId
     * @param context
     * @return
     */
    public CustomDialogBuilder setCustomView(int resId, Context context) {
        View customView = View.inflate(context, resId, null);
        if (mFrameLayoutCustomView.getChildCount() > 0) {
            mFrameLayoutCustomView.removeAllViews();
        }
        mFrameLayoutCustomView.addView(customView);
        return this;
    }

    /****
     * 设置添加额外的视图
     *
     * @param view
     * @param context
     * @return
     */
    public CustomDialogBuilder setCustomView(View view, Context context) {
        if (mFrameLayoutCustomView.getChildCount() > 0) {
            mFrameLayoutCustomView.removeAllViews();
        }
        mFrameLayoutCustomView.addView(view);
        return this;
    }

    /****************************************************** 设置添加视图(左右无边距) ****************************************/

    /****
     * 设置添加额外的视图
     *
     * @param resId
     * @param context
     * @return
     */
    public CustomDialogBuilder setNewCustomView(int resId, Context context) {
        View customView = View.inflate(context, resId, null);
        if (new_mFrameLayoutCustomView.getChildCount() > 0) {
            new_mFrameLayoutCustomView.removeAllViews();
        }
        new_mFrameLayoutCustomView.addView(customView);
        return this;
    }

    /****
     * 设置添加额外的视图
     *
     * @param view
     * @param context
     * @return
     */
    public CustomDialogBuilder setNewCustomView(View view, Context context) {
        if (new_mFrameLayoutCustomView.getChildCount() > 0) {
            new_mFrameLayoutCustomView.removeAllViews();
        }
        new_mFrameLayoutCustomView.addView(view);
        return this;
    }

    /********************* 额外添加的视图容器（没有边距） *************************/
    /****
     * 设置添加额外的视图
     *
     * @param resId
     * @param context
     * @return
     */
    public CustomDialogBuilder setNotPaddingCustomView(int resId, Context context) {
        new_mFrameLayoutCustomView.setVisibility(View.GONE);
        mLinearLayoutView.setBackgroundResource(R.color.trans);
        View customView = View.inflate(context, resId, null);
        if (mFrameLayoutCustomViewNotPadding.getChildCount() > 0) {
            mFrameLayoutCustomViewNotPadding.removeAllViews();
        }
        mFrameLayoutCustomViewNotPadding.addView(customView);
        return this;
    }

    /****
     * 设置添加额外的视图
     *
     * @param view
     * @param context
     * @return
     */
    public CustomDialogBuilder setNotPaddingCustomView(View view, Context context) {
        new_mFrameLayoutCustomView.setVisibility(View.GONE);
        mLinearLayoutView.setBackgroundResource(R.color.trans);
        if (mFrameLayoutCustomViewNotPadding.getChildCount() > 0) {
            mFrameLayoutCustomViewNotPadding.removeAllViews();
        }
        mFrameLayoutCustomViewNotPadding.addView(view);
        return this;
    }

    /**
     * 设置点击屏幕是否取消弹出框（true：取消；false：不取消）
     *
     * @param cancelable
     * @return
     */
    public CustomDialogBuilder isCancelableOnTouchOutside(boolean cancelable) {
        this.isCancelable = cancelable;
        dialog.setCanceledOnTouchOutside(cancelable);
        return this;
    }

    /***
     * 设置是否禁用返回按钮取消对话框（true：可取消；false：禁用，不可取消）
     *
     * @param cancelable
     * @return
     */
    public CustomDialogBuilder isCancelable(boolean cancelable) {
        this.isCancelable = cancelable;
        dialog.setCancelable(cancelable);
        return this;
    }

    private void toggleView(View view, Object obj) {
        if (obj == null) {
            view.setVisibility(View.GONE);
        } else {
            view.setVisibility(View.VISIBLE);
        }
    }


    /*****
     * 取消对话框
     */
    public void dismiss() {
        dialog.dismiss();

        mButton1.setVisibility(View.GONE);
        mButton2.setVisibility(View.GONE);
        mButton3.setVisibility(View.GONE);
        line_vertical.setVisibility(View.GONE);
    }

    /**
     * 监听关闭
     *
     * @param onDismissListener
     */
    public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
        dialog.setOnDismissListener(onDismissListener);
    }

    /***
     * 隐藏title
     */
    public CustomDialogBuilder hideTitle() {
        title_template.setVisibility(View.GONE);
        mDivider.setVisibility(View.GONE);
        return this;
    }

    /***
     * 显示title
     */
    public CustomDialogBuilder showTitle() {
        title_template.setVisibility(View.VISIBLE);
        mDivider.setVisibility(View.VISIBLE);
        return this;
    }

    public void show() {
        // TODO Auto-generated method stub
        dialog.show();
        if (dialog.isShowing()) {
            return;
        } else {
            this.show();
        }
    }
}
